package com.example.english.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.english.App;
import com.example.english.R;
import com.example.english.db.WordBean;

import java.util.ArrayList;
import java.util.List;

public class PhotogAdapter extends RecyclerView.Adapter<PhotogAdapter.T2Holder> {

    private List<WordBean> list = new ArrayList<>();

    public void setList(List<WordBean> list){
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public List<WordBean> getList(){
        return list;
    }

    public void add(WordBean bean){
        list.add(bean);
        notifyDataSetChanged();
    }



    @NonNull
    @Override
    public T2Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_photog,parent ,false);
        return new T2Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull T2Holder holder, int position) {
        holder.english.setText(list.get(position).getEnglish());
        holder.translate.setText(list.get(position).getTranslate());
        holder.img_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                App.dbsqLite.del(list.get(position));
                list.remove(position);
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class T2Holder extends RecyclerView.ViewHolder{

        private TextView english,translate ;
        private ImageView img_del;

        public T2Holder(@NonNull View itemView) {
            super(itemView);
            english = itemView.findViewById(R.id.english);
            translate = itemView.findViewById(R.id.translate);
            img_del = itemView.findViewById(R.id.img_del);
        }
    }

}
